package framework.view.components.bar
{
import com.greensock.TweenMax;

import flash.display.MovieClip;
import flash.display.Sprite;

import framework.proxy.ComponentProxy;

/**
 * ProgressBarProxy
 * @author jliang
 * 
 */
public class ProgressBarProxy extends ComponentProxy
{
	//==========================================================================
	//  Class constants
	//==========================================================================
	private static const THUMB_NAME:String = "thumb";
	private static const BASE_NAME:String = "base";
	//==========================================================================
	//  Variables
	//==========================================================================
	protected var thumb:Sprite;
	protected var base:Sprite;
	//==========================================================================
	//  Properties
	//==========================================================================	
    protected var _animationed:Boolean = false;

    public function get animationed():Boolean
    {
        return _animationed;
    }

    public function set animationed(value:Boolean):void
    {
        _animationed = value;
    }
    
    protected function get maxWidth():Number
    {
       // return (base.width - thumb.x * 2);
		return base.width;
    }
	
    protected var _maxValue:Number = Number.NaN;

	public function get maxValue():Number
	{
		return _maxValue;
	}

	public function set maxValue(value:Number):void
	{
		_maxValue = value;
		update();
	}
	
    protected var _minValue:Number = Number.NaN;

	public function get minValue():Number
	{
		return _minValue;
	}

	public function set minValue(value:Number):void
	{
		_minValue = value;
		update();
	}
	
    protected var _currentValue:Number = Number.NaN;

	public function get currentValue():Number
	{
		return _currentValue;
	}

	public function set currentValue(value:Number):void
	{
		_currentValue = value;
		update();
	}
	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================
	override public function parse(target:MovieClip):void
	{
		super.parse(target);
		
		thumb = skin.getChildByName(THUMB_NAME) as Sprite;
		base = skin.getChildByName(BASE_NAME) as Sprite;
		
		update();
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	override public function dispose():void
	{
		TweenMax.killTweensOf(thumb);
		
		super.dispose();
	}
	//==========================================================================
	//  Protected methods
	//==========================================================================
	protected function update():void
	{
		if (!skin || isNaN(maxValue) || isNaN(minValue))
		{
			return;
		}
        
        TweenMax.killTweensOf(thumb);
		
		var pect:Number = (currentValue - minValue) / (maxValue - minValue);
        var temp:Number = maxWidth * pect;
		if (temp > maxWidth) temp = maxWidth;
        if (animationed)
        {
    		if (temp > thumb.width)
            {
                TweenMax.to(thumb, 0.5, { width:temp } );
            }
            else
            {
                var d:Number = 0.5 * maxWidth / temp;
                TweenMax.to(thumb, d, { width:maxWidth, onComplete:function():void
                                                                    {
                                                                        thumb.width = 0;
                                                                        TweenMax.to(thumb, 0.7 - d, { width:temp } );
                                                                    }} );
                
            }
        }
        else
        {
            thumb.width = temp;
        }
	}

}
}





